# SQL系统项目Makefile

# 编译选项
CC = gcc
CFLAGS = -Wall -Wextra -g -finput-charset=utf-8 -fexec-charset=utf-8 -Iinclude/compiler -Iinclude/database -Iinclude/storage -Iinclude/common
LDFLAGS = -mconsole
TARGET = sql_system

# 目录结构
INCLUDE_DIR = include
SRC_DIR = src
TEST_DIR = test
DATA_DIR = data
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)\obj

# 查找所有源文件
COMPILER_SRCS = $(wildcard $(SRC_DIR)/compiler/*.c)
DATABASE_SRCS = $(wildcard $(SRC_DIR)/database/*.c)
STORAGE_SRCS = $(wildcard $(SRC_DIR)/storage/*.c)
COMPILER_SRCS := $(filter-out $(SRC_DIR)/compiler/main.c, $(COMPILER_SRCS))

# 生成目标文件列表，将.o文件放在OBJ_DIR下
COMPILER_OBJS = $(patsubst $(SRC_DIR)/compiler/%.c, $(OBJ_DIR)/compiler/%.o, $(COMPILER_SRCS))
DATABASE_OBJS = $(patsubst $(SRC_DIR)/database/%.c, $(OBJ_DIR)/database/%.o, $(DATABASE_SRCS))
STORAGE_OBJS = $(patsubst $(SRC_DIR)/storage/%.c, $(OBJ_DIR)/storage/%.o, $(STORAGE_SRCS))
ALL_OBJS = $(COMPILER_OBJS) $(DATABASE_OBJS) $(STORAGE_OBJS)

# 默认目标
all: directories $(TARGET)

# 创建必要的目录
.PHONY: directories
directories:
	@if not exist "$(DATA_DIR)" mkdir "$(DATA_DIR)"
	@if not exist "$(BUILD_DIR)" mkdir "$(BUILD_DIR)"
	@if not exist "$(OBJ_DIR)\compiler" mkdir "$(OBJ_DIR)\compiler"
	@if not exist "$(OBJ_DIR)\database" mkdir "$(OBJ_DIR)\database"
	@if not exist "$(OBJ_DIR)\storage" mkdir "$(OBJ_DIR)\storage"

# 编译主程序
$(TARGET): $(ALL_OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(BUILD_DIR)/$@

# 编译单个源文件，将.o文件放在OBJ_DIR下
$(OBJ_DIR)/compiler/%.o: $(SRC_DIR)/compiler/%.c
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/database/%.o: $(SRC_DIR)/database/%.c
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/storage/%.o: $(SRC_DIR)/storage/%.c
	$(CC) $(CFLAGS) -c $< -o $@

# 清理目标文件和可执行文件
clean:
	@if exist "$(OBJ_DIR)\compiler\*.o" del /F /Q "$(OBJ_DIR)\compiler\*.o"
	@if exist "$(OBJ_DIR)\database\*.o" del /F /Q "$(OBJ_DIR)\database\*.o"
	@if exist "$(OBJ_DIR)\storage\*.o" del /F /Q "$(OBJ_DIR)\storage\*.o"
	@if exist "$(BUILD_DIR)\$(TARGET).exe" del /F /Q "$(BUILD_DIR)\$(TARGET).exe"
	@if exist "$(BUILD_DIR)\test_lexer.exe" del /F /Q "$(BUILD_DIR)\test_lexer.exe"
	@if exist "$(BUILD_DIR)\test_database.exe" del /F /Q "$(BUILD_DIR)\test_database.exe"
	@if exist "$(BUILD_DIR)\test_disk_io.exe" del /F /Q "$(BUILD_DIR)\test_disk_io.exe"
	@if exist "$(BUILD_DIR)\test_storage.exe" del /F /Q "$(BUILD_DIR)\test_storage.exe"
	@if exist "$(BUILD_DIR)\test_table_operations.exe" del /F /Q "$(BUILD_DIR)\test_table_operations.exe"
	@echo "清理完成"

# 测试词法分析器
.PHONY: test_lexer
test_lexer:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_lexer.c $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/compiler/token.o -o $(BUILD_DIR)/test_lexer
	@echo "运行词法分析器测试..."
	@$(BUILD_DIR)/test_lexer

# 编译并运行数据库综合测试
.PHONY: test_database
test_database:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_database.c $(OBJ_DIR)/compiler/ast.o $(OBJ_DIR)/compiler/token.o $(OBJ_DIR)/compiler/parser.o $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/database/expr.o $(OBJ_DIR)/database/plan_generator.o $(OBJ_DIR)/database/catalog.o $(OBJ_DIR)/database/page.o $(OBJ_DIR)/database/record.o $(OBJ_DIR)/database/semantic_analyzer.o $(OBJ_DIR)/database/executor.o $(OBJ_DIR)/database/plan_utils.o $(OBJ_DIR)/storage/storage.o $(OBJ_DIR)/storage/disk_io.o $(OBJ_DIR)/storage/page_manager.o $(OBJ_DIR)/storage/cache_manager.o $(OBJ_DIR)/storage/stats_monitor.o $(OBJ_DIR)/storage/storage_system.o -o $(BUILD_DIR)/test_database
	@echo "运行数据库综合测试..."
	@$(BUILD_DIR)/test_database

# 运行特定测试
.PHONY: run_test_lexer
run_test_lexer:
	$(CC) $(CFLAGS) $(LDFLAGS) -Iinclude/compiler -o $(BUILD_DIR)/test_lexer $(TEST_DIR)/test_lexer.c $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/compiler/token.o
	@$(BUILD_DIR)/test_lexer

# 测试磁盘IO功能
.PHONY: test_disk_io
test_disk_io:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_disk_io.c $(OBJ_DIR)/storage/disk_io.o -o $(BUILD_DIR)/test_disk_io
	@echo "运行磁盘IO测试..."
	@$(BUILD_DIR)/test_disk_io

# 添加存储系统测试目标
.PHONY: test_storage
test_storage:
	@if not exist "$(BUILD_DIR)" mkdir "$(BUILD_DIR)"
	@if not exist "$(DATA_DIR)" mkdir "$(DATA_DIR)"
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_storage.c $(COMPILER_SRCS) $(filter-out $(SRC_DIR)/database/main.c, $(STORAGE_SRCS) $(DATABASE_SRCS)) -o $(BUILD_DIR)/test_storage
	@$(BUILD_DIR)/test_storage

# 更新test目标以包含存储系统测试
test: test_lexer test_database test_disk_io test_storage
	@echo "所有测试完成!"

# 清理测试程序和数据文件
.PHONY: clean_test
clean_test:
	@if exist "$(BUILD_DIR)\test_lexer.exe" del /f /q "$(BUILD_DIR)\test_lexer.exe"
	@if exist "$(BUILD_DIR)\test_database.exe" del /f /q "$(BUILD_DIR)\test_database.exe"
	@if exist "$(BUILD_DIR)\test_disk_io.exe" del /f /q "$(BUILD_DIR)\test_disk_io.exe"
	@if exist "$(BUILD_DIR)\test_storage.exe" del /f /q "$(BUILD_DIR)\test_storage.exe"
	@if exist "$(BUILD_DIR)\test_table_operations.exe" del /f /q "$(BUILD_DIR)\test_table_operations.exe"
	@if exist "$(DATA_DIR)\*.dat" del /f /q "$(DATA_DIR)\*.dat"
	@if exist "$(BUILD_DIR)\test_compiler.exe" del /F /Q "$(BUILD_DIR)\test_compiler.exe"
	@if exist "$(BUILD_DIR)\test_storage_system.exe" del /F /Q "$(BUILD_DIR)\test_storage_system.exe"
	@if exist "$(BUILD_DIR)\test_database_system.exe" del /F /Q "$(BUILD_DIR)\test_database_system.exe"
	@echo "测试程序和数据文件清理完成"

# 打印项目信息
.PHONY: info
info:
	@echo "SQL系统项目构建信息:"
	@echo "目标文件: $(TARGET)"
	@echo "源文件数: $(words $(COMPILER_SRCS) $(DATABASE_SRCS) $(STORAGE_SRCS))"
	@echo "编译器源文件: $(COMPILER_SRCS)"
	@echo "数据库源文件: $(DATABASE_SRCS)"
	@echo "存储系统源文件: $(STORAGE_SRCS)"

# 声明伪目标
.PHONY: all clean test test_lexer test_database test_disk_io run_test_lexer directories info clean_test test_storage test_table_operations

# 测试表操作功能
.PHONY: test_table_operations
test_table_operations:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_table_operations.c $(OBJ_DIR)/compiler/ast.o $(OBJ_DIR)/compiler/token.o $(OBJ_DIR)/compiler/parser.o $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/database/expr.o $(OBJ_DIR)/database/plan_generator.o $(OBJ_DIR)/database/catalog.o $(OBJ_DIR)/database/page.o $(OBJ_DIR)/database/record.o $(OBJ_DIR)/database/semantic_analyzer.o $(OBJ_DIR)/database/executor.o $(OBJ_DIR)/database/plan_utils.o $(OBJ_DIR)/storage/storage.o $(OBJ_DIR)/storage/disk_io.o $(OBJ_DIR)/storage/page_manager.o $(OBJ_DIR)/storage/cache_manager.o $(OBJ_DIR)/storage/stats_monitor.o $(OBJ_DIR)/storage/storage_system.o -o $(BUILD_DIR)/test_table_operations
	@echo "运行表操作测试..."
	@$(BUILD_DIR)/test_table_operations

# 编译并运行编译器综合测试
.PHONY: test_compiler
test_compiler:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_compiler.c $(OBJ_DIR)/compiler/ast.o $(OBJ_DIR)/compiler/token.o $(OBJ_DIR)/compiler/parser.o $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/database/expr.o $(OBJ_DIR)/database/plan_generator.o $(OBJ_DIR)/database/catalog.o $(OBJ_DIR)/database/page.o $(OBJ_DIR)/database/record.o $(OBJ_DIR)/database/semantic_analyzer.o $(OBJ_DIR)/database/executor.o $(OBJ_DIR)/database/plan_utils.o $(OBJ_DIR)/storage/storage.o $(OBJ_DIR)/storage/disk_io.o $(OBJ_DIR)/storage/page_manager.o $(OBJ_DIR)/storage/cache_manager.o $(OBJ_DIR)/storage/stats_monitor.o $(OBJ_DIR)/storage/storage_system.o -o $(BUILD_DIR)/test_compiler
	@echo "运行编译器综合测试..."
	@$(BUILD_DIR)/test_compiler

# 编译并运行存储系统测试
.PHONY: test_storage_system
test_storage_system:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_storage_system.c $(OBJ_DIR)/compiler/ast.o $(OBJ_DIR)/compiler/token.o $(OBJ_DIR)/compiler/parser.o $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/database/expr.o $(OBJ_DIR)/database/plan_generator.o $(OBJ_DIR)/database/catalog.o $(OBJ_DIR)/database/page.o $(OBJ_DIR)/database/record.o $(OBJ_DIR)/database/semantic_analyzer.o $(OBJ_DIR)/database/executor.o $(OBJ_DIR)/database/plan_utils.o $(OBJ_DIR)/storage/storage.o $(OBJ_DIR)/storage/disk_io.o $(OBJ_DIR)/storage/page_manager.o $(OBJ_DIR)/storage/cache_manager.o $(OBJ_DIR)/storage/stats_monitor.o $(OBJ_DIR)/storage/storage_system.o -o $(BUILD_DIR)/test_storage_system
	@echo "运行存储系统测试..."
	@$(BUILD_DIR)/test_storage_system

# 编译并运行数据库系统核心测试
.PHONY: test_database_system
test_database_system:
	$(CC) $(CFLAGS) $(LDFLAGS) $(TEST_DIR)/test_database_system.c $(OBJ_DIR)/compiler/ast.o $(OBJ_DIR)/compiler/token.o $(OBJ_DIR)/compiler/parser.o $(OBJ_DIR)/compiler/lexer.o $(OBJ_DIR)/database/expr.o $(OBJ_DIR)/database/plan_generator.o $(OBJ_DIR)/database/catalog.o $(OBJ_DIR)/database/page.o $(OBJ_DIR)/database/record.o $(OBJ_DIR)/database/semantic_analyzer.o $(OBJ_DIR)/database/executor.o $(OBJ_DIR)/database/plan_utils.o $(OBJ_DIR)/storage/storage.o $(OBJ_DIR)/storage/disk_io.o $(OBJ_DIR)/storage/page_manager.o $(OBJ_DIR)/storage/cache_manager.o $(OBJ_DIR)/storage/stats_monitor.o $(OBJ_DIR)/storage/storage_system.o -o $(BUILD_DIR)/test_database_system
	@echo "运行数据库系统核心测试..."
	@$(BUILD_DIR)/test_database_system

# 更新test目标以包含数据库系统核心测试
test: test_lexer test_database test_disk_io test_storage test_table_operations test_compiler test_storage_system test_database_system
	@echo "所有测试完成!"
	